import Axios from "axios";
import store from "@/store";
//导航条
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import {
    Message,
    MessageBox
} from 'element-ui'

export default function axios(config) {
    const instance = Axios.create({
        timeout: 20000
    });
    //本地地址
    // instance.defaults.baseURL = 'http://192.168.117.132:8888/api/private/v1/'
    //远程地址
    instance.defaults.baseURL = 'http://www.ysqorz.top:8888/api/private/v1/'
    //使用拦截器拦截全局loading状态
    instance.interceptors.request.use((config) => {
        //导航条展示
        NProgress.start();
        config.headers['Authorization'] = sessionStorage.getItem("token")
        store.commit('show_loading')
        return config
    })
    //使用拦截器拦截全局loading状态
    instance.interceptors.response.use(config => {
        //导航条隐藏
        NProgress.done()
        store.commit('hide_loading')
        return config
    })
    return instance(config).catch((res)=>{
        store.commit('hide_loading')
        Message({
            message: `${res.message}【请求超时】`,
            type: 'error',
            duration: 5 * 1000
        })
    })
}
